package com.hzit.manager.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;


import com.hzit.manager.api.domain.Goods;
import com.hzit.manager.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.hzit.manager.service.IGoodsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商品Controller
 * 
 * @author wf
 * @date 2023-08-29
 */
@RestController
@RequestMapping("/goods")
public class GoodsController extends BaseController
{
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IItemService itemService;

    /**
     * 查询商品列表
     */
    @RequiresPermissions("manager:goods:list")
    @GetMapping("/list")
    public TableDataInfo list(Goods goods) throws InvocationTargetException, IllegalAccessException {
        startPage();
        List<Goods> list = goodsService.selectGoodsList(goods);
        return getDataTable(list);
    }

    /**
     * 1. 进行商品的审核
     * @param id
     * @param status  tb_goods(spu)--->sku(tb_item)
     * @return
     */
    @GetMapping("/updateStatus/{id}/{status}")
    public AjaxResult updateStatus(@PathVariable("id") Long id,
                                   @PathVariable("status") String status){
        //1. 进行商品审核
        Goods goods = new Goods();
        goods.setId(id);
        goods.setAuditStatus(status);
        goodsService.updateGoods(goods);

        //2. 根据商品id查询sku商品列表
        itemService.sendMessageToES(id);
        //3. 返回AjaxResult对象
        return AjaxResult.success("商品审核成功！");
    }
    /**
     * 导出商品列表
     */
    @RequiresPermissions("manager:goods:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Goods goods) throws InvocationTargetException, IllegalAccessException {
        List<Goods> list = goodsService.selectGoodsList(goods);
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    // @RequiresPermissions("manager:goods:query")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        Goods goods = goodsService.selectGoodsById(id);
        System.out.println("goods = " + goods);
        return AjaxResult.success(goods);
    }

    /**
     * 新增商品
     */
    @RequiresPermissions("manager:goods:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Goods goods)
    {
        return toAjax(goodsService.insertGoods(goods));
    }

    /**
     * 修改商品
     */
    @RequiresPermissions("manager:goods:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Goods goods)
    {
        return toAjax(goodsService.updateGoods(goods));
    }

    /**
     * 删除商品
     */
    @RequiresPermissions("manager:goods:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(goodsService.deleteGoodsByIds(ids));
    }
}
